﻿using System;
using System.Configuration;
using System.Globalization;
using System.Threading;
using System.Data;
using System.Web;
using KPIS.GERP.ESS.BLL;
using System.Web.UI;

namespace KPIS.GERP.ESS.APP
{
    public partial class SIGDICLogin : System.Web.UI.Page
    {
        private B_SECURITY_Users userBLL = new B_SECURITY_Users();
        FIM_SystemControls.FIM_SystemControls sc = new FIM_SystemControls.FIM_SystemControls();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string strUserID = Request.QueryString["id"];
                string strPassword = Request.QueryString["pw"];

                if (strUserID != null)
                {
                    hHeight.Value = "700";
                    Login1(strUserID, strPassword);
                }
            }
        }

        protected void cmdLogin_Click(object sender, EventArgs e)
        {
            string strUserID = Email.Value.ToUpper().Trim();
            string strPassword = Passwd.Value.Trim();

            Login1(strUserID, strPassword);
        }

        protected void Login1(string strUserID, string strPassword)
        {
            if (strUserID.Length == 0)
            {
                lblError.Text = "กรุณากรอก [รหัสประจำตัว]";
                return;
            }
            else if (strPassword.Length == 0)
            {
                lblError.Text = "กรุณากรอก [รหัสผ่าน]";
                return;
            }

            if (!CheckPassword(strUserID, strPassword))
            {
                lblError.Text = "[รหัสผ่าน] ไม่ถูกต้อง";
                return;
            }
            else
            {
                Response.Redirect(@"~\App_UI\Forms\SelectSystem.aspx");
            }
        }

        private bool CheckPassword(string strUserID, string strPassword)
        {
            bool bOK = false;
            //DataSet ds = security.Login(strUserID, strPassword);

            DataTable dt = userBLL.GetUserInfo(strUserID, strPassword);

            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];

                Session["EmpSeq"] = dr["emp_seq"].ToString();
                Session["UserSeq"] = dr["user_seq"].ToString();
                Session["WorkingJobSiteSeq"] = dr["working_job_site_seq"].ToString();
                Session["WorkingJobSiteName"] = dr["working_job_site_name"].ToString();
                Session["WorkAreaSeq"] = dr["work_area_seq"].ToString();
                Session["Height"] = hHeight.Value;
                Session["username"] = dr["user_name"].ToString();

                HttpCookie cookie = new HttpCookie("UserDetail");
                cookie["EmpSeq"] = dr["emp_seq"].ToString();
                cookie["UserName"] = dr["user_name"].ToString();
                cookie["UserSeq"] = dr["user_seq"].ToString();
                cookie["WorkingJobSiteSeq"] = dr["working_job_site_seq"].ToString();
                cookie["Height"] = hHeight.Value;
                Response.Cookies.Add(cookie);
                SetSystemControl();
                bOK = true;
            }

            return bOK;
        }

        private void SetSystemControl()
        {
            Session["SCInfo"] = sc.GetInfo();
        }
    }
}